package com.bbcare.department.plat.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.bbcare.comm.Constants;
import com.bbcare.comm.DateUtil;
import com.bbcare.department.plat.service.IServiceManagerService;
import com.bbcare.department.plat.store.ibatis.IServiceManagerDao;
import com.bbcare.followup.plat.service.IFollowQueueService;
import com.bbcare.followup.plat.store.ibatis.IFollowQueueDAO;
import com.bbcare.followup.plat.store.ibatis.IPatientQueueSchemeDAO;

@Service
public class ServiceManagerService implements IServiceManagerService {
	
	Logger logger = Logger.getLogger(ServiceManagerService.class);

	@Autowired
    private IFollowQueueDAO queueDao;
	
	@Autowired
    private IServiceManagerDao serviceManagerDao;
	
    @Autowired
    private IPatientQueueSchemeDAO patientQueueSchemeDao;
    
    @Autowired
    private IFollowQueueService followQueueService;
	
	@Override
	public List<Map<String, Object>> getServicePatientTrendStatisics(
			Map<String, Object> paramMap) throws Exception {
		String type = null == paramMap.get("type") ? "":(String)paramMap.get("type");
		String flag = null == paramMap.get("flag") ? "":(String)paramMap.get("flag");

        List<Map<String,Object>> rsplist =new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> rsplistN =new ArrayList<Map<String,Object>>();
        if("1".equals(type)){//开通
        	if("1".equals(flag)){//按月
    			//最近6月 按年月
    			rsplist = serviceManagerDao.countServiceOpenUsedByMonth(paramMap);
    			String [] months = DateUtil.getLast6Months();
    			
    			Map<String,Object> mapQry = new HashMap<String,Object>();
    			for(int j=0;j<rsplist.size();j++){
    				mapQry.put(rsplist.get(j).get("date").toString(),rsplist.get(j).get("openCount"));
    			}
    			for(int i = 0;i<months.length ;i++){
    				Map<String,Object> mapTemp = new HashMap<String,Object>();
    				if(!mapQry.containsKey(months[i])){
    					mapTemp.put("count",0);
    					mapTemp.put("date",months[i]);
    					rsplistN.add(mapTemp);
    				}else{
    					mapTemp.put("count",mapQry.get(months[i]));
    					mapTemp.put("date",months[i]);
    					rsplistN.add(mapTemp);
    				}
    			}
    			return rsplistN;
    		}else{
    			//最近7天  按年月日
    			rsplist = serviceManagerDao.countServiceOpenUsedByDay(paramMap);
    			
    			String [] days = DateUtil.getLast7days();
    			
    			Map<String,Object> mapQry = new HashMap<String,Object>();
    			for(int j=0;j<rsplist.size();j++){
    				mapQry.put(rsplist.get(j).get("date").toString(),rsplist.get(j).get("openCount"));
    			}
    			for(int i = 0;i<days.length ;i++){
    				Map<String,Object> mapTemp = new HashMap<String,Object>();
    				if(!mapQry.containsKey(days[i])){
    					mapTemp.put("count",0);
    					mapTemp.put("date",days[i]);
    					rsplistN.add(mapTemp);
    				}else{
    					mapTemp.put("count",mapQry.get(days[i]));
    					mapTemp.put("date",days[i]);
    					rsplistN.add(mapTemp);
    				}
    			}
    			return rsplistN;
    		}
        }else{
        	if("1".equals(flag)){//按月
    			//最近6月 按年月
    			rsplist = serviceManagerDao.countServiceOpenUsedByMonth(paramMap);
    			String [] months = DateUtil.getLast6Months();
    			
    			Map<String,Object> mapQry = new HashMap<String,Object>();
    			for(int j=0;j<rsplist.size();j++){
    				mapQry.put(rsplist.get(j).get("date").toString(),rsplist.get(j).get("useCount"));
    			}
    			for(int i = 0;i<months.length ;i++){
    				Map<String,Object> mapTemp = new HashMap<String,Object>();
    				if(!mapQry.containsKey(months[i])){
    					mapTemp.put("count",0);
    					mapTemp.put("date",months[i]);
    					rsplistN.add(mapTemp);
    				}else{
    					mapTemp.put("count",mapQry.get(months[i]));
    					mapTemp.put("date",months[i]);
    					rsplistN.add(mapTemp);
    				}
    			}
    			return rsplistN;
    		}else{
    			//最近7天  按年月日
    			rsplist = serviceManagerDao.countServiceOpenUsedByDay(paramMap);
    			
    			String [] days = DateUtil.getLast7days();
    			
    			Map<String,Object> mapQry = new HashMap<String,Object>();
    			for(int j=0;j<rsplist.size();j++){
    				mapQry.put(rsplist.get(j).get("date").toString(),rsplist.get(j).get("useCount"));
    			}
    			for(int i = 0;i<days.length ;i++){
    				Map<String,Object> mapTemp = new HashMap<String,Object>();
    				if(!mapQry.containsKey(days[i])){
    					mapTemp.put("count",0);
    					mapTemp.put("date",days[i]);
    					rsplistN.add(mapTemp);
    				}else{
    					mapTemp.put("count",mapQry.get(days[i]));
    					mapTemp.put("date",days[i]);
    					rsplistN.add(mapTemp);
    				}
    			}
    			return rsplistN;
    		}
        }
		
	}

	@Override
	public List<Map<String, Object>> getUseStatistics(Map<String, Object> paramMap)
			throws Exception {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		//数据解读数据
		paramMap.put("encode",Constants.PRODUCTITEM_TYPE_DATA_INTERPRETAION);
		List<Map<String, Object>> queryList =  serviceManagerDao.countServiceUsagePatientByDeptId(paramMap);
		
		int countOpen = 0 , countuse = 0;
		Map<String, Object> interpretationMap = new HashMap<String, Object>();
		if(!CollectionUtils.isEmpty(queryList)){
			//status  1/2 为使用中  1 2 3 为已开通
			for(int i= 0 ;i<queryList.size();i++){
				String status = (String)queryList.get(i).get("status");
				if(status.endsWith("1") || status.endsWith("2")){
					countOpen+=Integer.parseInt((String)queryList.get(i).get("count"));
					countuse+=Integer.parseInt((String)queryList.get(i).get("count"));
				}
				if(status.endsWith("3")){
					countOpen+=Integer.parseInt((String)queryList.get(i).get("count"));
				}
			}
		}
		
		interpretationMap.put("openServiceNum",countOpen);
		interpretationMap.put("serviceType","2");
		interpretationMap.put("useServiceNum",countuse);
		
		int onlineCountOpen = 0 ; int onlineCountuse = 0;
		queryList = null;
		paramMap.put("encode",Constants.PRODUCTITEM_TYPE_ONLINE_CONSULT);
		queryList =  serviceManagerDao.countServiceUsagePatientByDeptId(paramMap);
		if(!CollectionUtils.isEmpty(queryList)){
			//status  1/2 为使用中  1 2 3 为已开通
			for(int i= 0 ;i<queryList.size();i++){
				String status = String.valueOf(queryList.get(i).get("status"));
				if(status.endsWith("1") || status.endsWith("2")){
					onlineCountOpen+=Integer.parseInt(String.valueOf(queryList.get(i).get("count")));
					onlineCountuse+=Integer.parseInt(String.valueOf(queryList.get(i).get("count")));
				}
				if(status.endsWith("3")){
					onlineCountOpen+=Integer.parseInt(String.valueOf(queryList.get(i).get("count")));
				}
			}
		}
		Map<String, Object> onlineMap = new HashMap<String, Object>();
		onlineMap.put("openServiceNum",onlineCountOpen);
		onlineMap.put("serviceType","1");
		onlineMap.put("useServiceNum",onlineCountuse);
		
		list.add(onlineMap);
		list.add(interpretationMap);
		return list;
	}

	@Override
	public Map<String, Object> getPatientTotal(Map<String, Object> paramMap)
			throws Exception {
		Map<String, Object> rspMap = new HashMap<String, Object>();
		
		int familymanagerCount=0;
		int freeCount=0;
		int singleConsultationCount=0;
		int singleReading=0;

		List<String> queueIdArray = followQueueService.getQueueArrByAuthority(paramMap);
        if(CollectionUtils.isEmpty(queueIdArray)){
        	logger.info("该用户无可查看的队列|患者");
        	rspMap.put("familymanagerCount", familymanagerCount);
     		rspMap.put("freeCount", freeCount);
     		rspMap.put("singleConsultationCount", singleConsultationCount);
     		rspMap.put("singleReading", singleReading);
     		return rspMap;
        }
        paramMap.put("queueIdIn", queueIdArray);
        paramMap.put("userStatus", Constants.PATIENT_QUEUESCHEME_USER_STATUS_NOMAL);// 队列中正常状态下的用户

        // 患者总数
        freeCount = patientQueueSchemeDao.countTotalPtQueueNumByCondition(paramMap);// 患者同时加入多个队列
        
        Map<String, Object> qryPaitentQueueMap = new HashMap<String, Object>();
        qryPaitentQueueMap.put("deptId",paramMap.get("deptId"));
        List<String> encodes = new ArrayList<String>();
        //家庭管理患者
//        encodes.add(Constants.PRODUCTITEM_TYPE_ONLINE_CONSULT);
        encodes = new ArrayList<String>();
//        qryPaitentQueueMap.put("encodes", encodes);
        qryPaitentQueueMap.put("type", "2");
        familymanagerCount = serviceManagerDao.countFamilyServicePatientByDeptId(qryPaitentQueueMap);// 患者同时加入多个队列
        
        //单次咨询患者
        encodes = new ArrayList<String>();
        encodes.add(Constants.PRODUCTITEM_TYPE_ONLINE_CONSULT);
        encodes.add(Constants.PRODUCTITEM_TYPE_PHONE_CONSULT);
        qryPaitentQueueMap.put("encodes", encodes);
        qryPaitentQueueMap.put("type", "3");
        singleConsultationCount = serviceManagerDao.countServicePatientByDeptId(qryPaitentQueueMap);// 患者同时加入多个队列
        
        //单次解读患者
        encodes = new ArrayList<String>();
        encodes.add(Constants.PRODUCTITEM_TYPE_DATA_INTERPRETAION);
        qryPaitentQueueMap.put("encodes", encodes);
        qryPaitentQueueMap.put("type", "3");// 服务类型  2 套餐 3 单次
        singleReading = serviceManagerDao.countServicePatientByDeptId(qryPaitentQueueMap);// 患者同时加入多个队列
       
        rspMap.put("familymanagerCount", familymanagerCount);
		rspMap.put("freeCount", freeCount);
		rspMap.put("singleConsultationCount", singleConsultationCount);
		rspMap.put("singleReading", singleReading);
		return rspMap;
	} 
	
}
